package com.macro.mall.portal.controller;

import com.macro.mall.portal.domain.MongoProduct;
import com.macro.mall.portal.service.MongoProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 测试连接：
 * 访问 GET /apimg/products/health 返回 true 表示连接成功
 *
 * 查询数据：
 * 获取所有产品：GET /apimg/products
 * 条件查询：GET /apimg/products/search?name=手机&maxPrice=5000&category=电子产品
 *
 * 增加数据：
 * 添加单个产品：POST /apimg/products
 * 批量添加：POST /apimg/products/batch
 *
 * 删除数据：
 * 按ID删除：DELETE /apimg/products/{id}
 * 按分类删除：DELETE /apimg/products/category/{category}
 */


@RestController
@RequestMapping("/apimg/products")
public class MongoProductController {
    @Autowired
    private MongoProductService productService;

    // 测试连接
    @GetMapping("/health")
    public boolean checkHealth() {
        System.out.println("Checking MongoDB health...");
        return productService.checkMongoConnection();
    }

    // 测试连接
    @GetMapping("/health2")
    public String checkHealth2() {
        System.out.println("Checking MongoDB health22..."+productService.testConnection());
        //输出结果到浏览器中显示
        System.out.println(productService.testConnection());

        return productService.testConnection();
    }

    @GetMapping("/{id}")
    public MongoProduct getProductById(@PathVariable String id) {
        return productService.findProductById(id);
    }

//    @GetMapping("/search")
//    public List<Product> searchProducts(
//            @RequestParam(required = false) String name,
//            @RequestParam(required = false) BigDecimal maxPrice,
//            @RequestParam(required = false) String category) {
//        return productService.searchProducts(name, maxPrice, category);
//    }

    // 增加
    @PostMapping
    public MongoProduct addProduct(@RequestBody MongoProduct product) {
        return productService.addProduct(product);
    }

    @PostMapping("/batch")
    public List<MongoProduct> addProducts(@RequestBody List<MongoProduct> products) {
        return productService.addProducts(products);
    }

    // 删除
    @DeleteMapping("/{id}")
    public String deleteProduct(@PathVariable String id) {
        productService.deleteProductById(id);
        return "Deleted product with ID: " + id;
    }

//    @DeleteMapping("/category/{category}")
//    public long deleteByCategory(@PathVariable String category) {
//        return productService.deleteProductsByCategory(category);
//    }

    // 1. 查询所有产品
    @GetMapping
    public List<MongoProduct> getAllProducts() {
        System.out.println("Getting all products...");
        return productService.findAllProducts();
    }

    // 2. 按名称搜索产品
    @GetMapping("/search")
    public List<MongoProduct> searchProductsByName(@RequestParam String name) {
        return productService.searchProductsByName(name);
    }

    // 3. 按分类获取产品
    @GetMapping("/category")
    public List<MongoProduct> getProductsByCategory(@RequestParam String category) {
        return productService.getProductsByCategory(category);
    }

    // 4. 插入/更新产品（POST请求）
//    @PostMapping
//    public CommonResult<Product> saveProduct(@RequestBody Product product) {
//        //新建一个数给
//        return CommonResult.success(product);
////        return productService.saveProduct(product);
//    }

    // 5. 按名称删除产品
    @DeleteMapping("/name")
    public String deleteProductsByName(@RequestParam String name) {
        List<MongoProduct> count = productService.deleteProductsByName(name);
        return "Deleted " + count + " products with name: " + name;
    }

}